/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.zhihu.gkfb.common;

import cn.ac.iie.di.zhihu.gkfb.configuration.Configuration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String JETTY_SERVER = "jettyServer";
    public static final String JETTY_PORT = "jettyPort";
    public static final String JETTY_THREADNUM = "jettyThreadNum";
    public static final String URI = "uri";
    public static final String SECRET = "secret";
    public static final String HTTPURL = "httpUrl";
    public static LinkedBlockingQueue<String> queueBlockingQueue = new LinkedBlockingQueue(200);//用于接收各个handler的返回值，以便mongo入库线程处理入库，数量根据具体入库线程和键值确定
    private static final List<Object> DATA_COLUMN_TYPE = new ArrayList<>();
    public static List<String> COLUMNS_NAME_LIST = new ArrayList<>();
    public static final String TOPIC = "topic";
    public static final String CONSUMER_NAMESERVER = "cosumer_nameserver";
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();

    public final static String mykeystore = "";
    public final static String keyStorePassword = "keyStorePassword";
    public final static String keyManagerPassword = "keyManagerPassword";
    public final static String trustStore = "trustStore";
    public final static String trustStorePassword = "trustStorePassword";
    public static ArrayBlockingQueue<String> dataPool = new ArrayBlockingQueue(4096);
//    public final static AtomicInteger countRequest = new AtomicInteger(0);
    
    public final static String fileSize = "fileSize";
    public final static String timeOut = "timeOut";
    //写文件线程个数
    public final static String writeJsonThreadSize = "writeJsonThreadSize";
    //文件生成路径
    public final static String filePath = "filePath";
    public final static String catchFilePath = "catchFilePath";
    public final static boolean isShouldExit = false;
    public final static String dataPoolSize = "dataPoolSize";
    public final static String address = "address";
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        setKeyAndValue(JETTY_PORT, "jettyPort");
        setKeyAndValue(JETTY_SERVER, "jettyServer");
        setKeyAndValue(JETTY_THREADNUM, "jettyThreadNum");
        setKeyAndValue(URI, "uri");
        setKeyAndValue(SECRET, "secret");
        setKeyAndValue(HTTPURL, "httpUrl");
        setKeyAndValue(keyStorePassword, "keyStorePassword");
        setKeyAndValue(trustStore, "trustStore");
        setKeyAndValue(trustStorePassword, "trustStorePassword");
        setKeyAndValue(keyManagerPassword, "keyManagerPassword");
        String[] typevalue = conf.getString("column.type", "").split(",");
        for (int i = 0; i < typevalue.length; i++) {
            DATA_COLUMN_TYPE.add(typevalue[i]);
        }
        setKeyAndValue(TOPIC, "topic");
        setKeyAndValue(CONSUMER_NAMESERVER, "cosumer_nameserver");
        setKeyAndValue(writeJsonThreadSize, "writeJsonThreadSize");
        setKeyAndValue(address, "address");
        setKeyAndValue(catchFilePath, "catchFilePath");
        setKeyAndValue(filePath, "filePath");
        setKeyAndValue(timeOut, "timeOut");
        setKeyAndValue(fileSize, "fileSize");
        setKeyAndValue(dataPoolSize, "dataPoolSize");
        
        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    /**
     *
     * @param columns_name_list 初始化消息队列的字段名称及其对应的类型
     * @param columns_type_list
     */
    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
